package com.crm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.dto.CourseDto;
import com.crm.dto.CoursePageDto;
import com.crm.mapper.CourseMapper;
import com.crm.entity.Course;
import com.crm.service.CourseService;
import com.crm.utils.CodeGeneratorUtil;
import com.crm.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public  class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;


    //添加课程
    @Override
    public void add(CourseDto courseDto) {
        Course course = new Course();

        course.setName(courseDto.getName());
        String code;
        do {
            code = CodeGeneratorUtil.generateRandomCode(8); // 生成8位随机编号
        } while (courseMapper.existsByCode(code)); // 检查是否已存在
        course.setCode(code);
        course.setCode(course.getCode());
        course.setInfo(courseDto.getInfo());
        course.setPrice(courseDto.getPrice());
        course.setSubject(courseDto.getSubject());
        course.setApplicablePerson(courseDto.getApplicablePerson());
        course.setCreateBy(courseDto.getCreateBy());
        courseMapper.insert(course);
        log.info("添加课程成功");
    }


    //修改
    @Override
    public void updateCourse(Course course) {
        courseMapper.updateById(course);
    }


    //批量删除
    @Override
    public boolean batchDeleteCourses(Long[] ids) {
        if (ids == null ) {
            throw new IllegalArgumentException("删除的课程ID列表不能为空");
        }
        //boolean result = super.removeByIds(ids);

        log.info("批量删除课程完成，共删除 {} 条记录", ids.length);
        return true;
    }


    //查询所有课程
    @Override
    public IPage<Course> getAllCourses(Page<Course> page) {
        return page(page);
    }


    //条件查询课程
    @Override
    public Page<Course> getCoursesByCondition(CoursePageDto coursePageDto) {
        Page<Course> page = new Page<>(coursePageDto.getPageNum(),coursePageDto.getPageSize());
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(coursePageDto.getName())) {
            wrapper.like(Course::getName, coursePageDto.getName()); // 模糊匹配名称
        }

        if (!StringUtils.isEmpty(coursePageDto.getCode())) {
            wrapper.eq(Course::getCode, coursePageDto.getCode()); // 精确匹配学科
        }
        if (coursePageDto.getSubject() != null){
            wrapper.eq(Course::getSubject, coursePageDto.getSubject());
        }
        if (coursePageDto.getApplicablePerson() != null){
            wrapper.eq(Course::getApplicablePerson, coursePageDto.getApplicablePerson());
        }
        //todo 时间范围查询
        Object beginTime = coursePageDto.getParams().get("beginCreateTime");
        Object endTime = coursePageDto.getParams().get("endCreateTime");

        if (beginTime != null && endTime != null &&
                !beginTime.toString().trim().isEmpty() &&
                !endTime.toString().trim().isEmpty()){
            wrapper.between(Course::getCreateTime, beginTime, endTime);
        }
        return courseMapper.selectPage(page, wrapper);
    }



    @Override
    public List<Course> getCoursesBySubject(String subject) {
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Course::getSubject, subject); // 精确匹配学科
        return courseMapper.selectList(wrapper);
    }

    @Override
    public List<Course> selectAll() {
        List<Course> courses = courseMapper.selectList(null);
        return courses;
    }

    @Override
    public Course getById(Long id) {
        return courseMapper.selectById(id);
    }



}
